#include "buzzer.h"

#define BUZZER_FREQ         (4 * 1000)
static void timer_init(void)
{
    TIM_InitTypeDef __xdata TIM_InitStructure;
    TIM_InitStructure.TIM_Mode = TIM_16BitAutoReload;
    TIM_InitStructure.TIM_Priority = Priority_1;
    TIM_InitStructure.TIM_Interrupt = DISABLE;
    TIM_InitStructure.TIM_ClkSource = TIM_CLOCK_12T;
    TIM_InitStructure.TIM_ClkOut = DISABLE;
    TIM_InitStructure.TIM_Value = 65535 - (MAIN_Fosc / 12 / 1000 / 1000 * 125);
    TIM_InitStructure.TIM_Run = DISABLE;
    Timer_Inilize(Timer3, &TIM_InitStructure);
}

void buzzer_func(void)
{
    if(sysParams.buzzer == 1)
    {
        Timer3_CLKO_Enable();
        Timer3_Run();
        sysParams.buzzer = 0;
    }
    else
    {
        Timer3_Stop();
        Timer3_CLKO_Disable();
        BUZZER_OFF();
    }
}

void buzzer_init(void)
{
    timer_init();
    GPIO_InitTypeDef __xdata GPIO_InitStructure;        //结构定义
    GPIO_InitStructure.Pin = GPIO_Pin_5;        //指定要初始化的IO,
    GPIO_InitStructure.Mode = GPIO_OUT_PP;      //指定IO的输入或输出方式
    GPIO_Inilize(GPIO_P0, &GPIO_InitStructure); //初始化
    BUZZER_OFF();
}

